<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品基本信息维度
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 * @version 1.0
 */


class goodsBasic {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }
    
    
    /**
     * 商品基本信息查询
     */
    public function searchBasics(&$arg_array) {
    
    	//sql字符串
    	$sql = '';
    
    	//参数类型字符串
    	$params_type = '';
    
    	//参数值数组
    	$params_array = array();
    
    	//基本信息ID
    	if (isset($arg_array['basicID']) && is_numeric($arg_array['basicID'])) {
    		$sql .= ' AND basicID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['basicID'];
    	}
    
    	//基本信息组ID
    	if (isset($arg_array['basicGroupID']) && is_numeric($arg_array['basicGroupID'])) {
    		$sql .= ' AND basicGroupID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['basicGroupID'];
    	}
    
    	//基本信息
    	if (isset($arg_array['basicName']) && !empty($arg_array['basicName'])) {
    		$sql .= ' AND basicName LIKE ?';
    		$params_type .= 's';
    		$params_array[] = $arg_array['basicName'];
    	}
    	
    	//基本信息值
    	if (isset($arg_array['basicVal']) && !empty($arg_array['basicVal'])) {
    		$sql .= ' AND basicVal LIKE ?';
    		$params_type .= 's';
    		$params_array[] = $arg_array['basicVal'];
    	}
    
    	//维度法文
    	if (isset($arg_array['french']) && trim($arg_array['french']) != '') {
    		$sql .= ' AND french LIKE ?';
    		$params_type .= 's';
    		$params_array[] = '%' . trim($arg_array['french']) . '%';
    	}
    
    	//维度中文
    	if (isset($arg_array['chinese']) && trim($arg_array['chinese']) != '') {
    		$sql .= ' AND chinese LIKE ?';
    		$params_type .= 's';
    		$params_array[] = '%' . trim($arg_array['chinese']) . '%';
    	}
    
    	//使用标志
    	if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg']) && in_array($arg_array['useFlg'],array(0,1))) {
    		$sql .= ' AND useFlg = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['useFlg'];
    	}
    
    	if ($sql) {
    		$sql = ' WHERE ' . substr($sql, 4);
    	}
    
    	try {
    
    		if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
    
    			//统计促销行数
    			$search_sql  = 'SELECT COUNT(*) AS count FROM goods_basic ' . $sql;
    			$count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    			$item_count  = $count_array[0]->count;
    			if ($item_count == 0) {
    				$arg_array['count'] = 0;
    				return null;
    			}
    
    			$arg_array['count'] = $item_count;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    			//分页
    			if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
    				$arg_array['page'] = 1;
    
    			$sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
    
    		} else {
    
    			$arg_array['count'] = 0;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname'])) {
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    			}
    		}
    
    		//查询商品促销信息
    
    		$search_sql = 'SELECT * FROM goods_basic' . $sql;
    		$temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    
    		if ($temp_item_array) {
    			foreach($temp_item_array as &$item) {
    				$item_array[] = array(
						'basicID' 	    => $item->basicID,
						'basicName' 	=> $item->basicName,
						'basicVal' 		=> $item->basicVal,
						'basicGroupID' 	=> $item->basicGroupID,
						'chinese' 	    => $item->chinese,
						'french'        => $item->french,
						'sortOrder'     => $item->sortOrder,
						'useFlg' 	    => $item->useFlg
    				);
    			}
    		}
    
    		return isset($item_array) ? $item_array : null;
    
    	} catch (Exception $e) {
    		error_log($e->getMessage());
    		return null;
    	}
    }
    
    /**
     * 商品基本信息组查询
     */
    public function searchgoodsBasic(&$arg_array) {
    	//sql字符串
    	$sql = '';
    
    	//参数类型字符串
    	$params_type = '';
    
    	//参数值数组
    	$params_array = array();
    
    	//基本信息组ID
    	if (isset($arg_array['basicGroupID']) && is_numeric($arg_array['basicGroupID'])) {
    		$sql .= ' AND basicGroupID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['basicGroupID'];
    	}
    
    	//维度法文
    	if (isset($arg_array['french']) && trim($arg_array['french']) != '') {
    		$sql .= ' AND french LIKE ?';
    		$params_type .= 's';
    		$params_array[] = '%' . trim($arg_array['french']) . '%';
    	}
    
    	//维度中文
    	if (isset($arg_array['chinese']) && trim($arg_array['chinese']) != '') {
    		$sql .= ' AND chinese LIKE ?';
    		$params_type .= 's';
    		$params_array[] = '%' . trim($arg_array['chinese']) . '%';
    	}
    
    	//使用标志
    	if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg']) && in_array($arg_array['useFlg'],array(0,1))) {
    		$sql .= ' AND useFlg = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['useFlg'];
    	}
    
    	if ($sql) {
    		$sql = ' WHERE ' . substr($sql, 4);
    	}
    
    	try {
    
    		if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
    
    			//统计促销行数
    			$search_sql  = 'SELECT COUNT(*) AS count FROM goods_basic_group ' . $sql;
    			$count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    			$item_count  = $count_array[0]->count;
    			if ($item_count == 0) {
    				$arg_array['count'] = 0;
    				return null;
    			}
    
    			$arg_array['count'] = $item_count;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY goods_basic_group.%s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    			//分页
    			if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
    				$arg_array['page'] = 1;
    
    			$sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
    
    		} else {
    
    			$arg_array['count'] = 0;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY goods_basic_group.%s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    		}
    
    		//查询商品基本信息
    
    		$search_sql = 'SELECT basicGroupID,chinese,french,sortOrder,useFlg FROM goods_basic_group' . $sql;
    		$temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    
    		if ($temp_item_array) {
    			foreach($temp_item_array as &$item) {
    
    				//根据基本信息组ID，查找基本信息
    				$goods_basic_array = array(
    						'basicGroupID' 	=> $item->basicGroupID
    				);
    
    				$goods_basic = $this->searchBasics($goods_basic_array);
    
    
    				$item_array[] = array(
    						'basicGroupID' 	=> $item->basicGroupID,
    						'chinese' 	    => $item->chinese,
    						'french'        => $item->french,
    						'sortOrder'     => $item->sortOrder,
    						'useFlg' 	    => $item->useFlg,
    						'goods_basic'   => $goods_basic
    				);
    			}
    		}
    
    		return isset($item_array) ? $item_array : null;
    
    	} catch (Exception $e) {
    
    		error_log($e->getMessage());
    		return null;
    
    	}
    }
    
    /**
     * 获取基本信息组ID的序号
     */
    public function searchSort() {
    
    	$maxID = $this->dbca->stmt_query('SELECT max(sortOrder) as maxID FROM goods_basic_group');
    
    	$maxID = $maxID[0]->maxID + 1;
    
    	return $maxID;
    }
    
    /**
     * 商品基本信息维度的保存/修改
     */
    public function savegoodsBasic($arg_array) {
        global $G_PGLOB;
    
    	/**
    	 * 检查参数
    	 */
    	//定义错误变量
    	$error_msg = '';
    
    	if (isset($arg_array['basicGroupID']) && !empty($arg_array['basicGroupID'])) {
    		$basicGroupID = $arg_array['basicGroupID'];
    	} else {
    		$basicGroupID = 0;
    	}
    
    	if (isset($arg_array['french']) && !empty($arg_array['french'])) {
    		$french = $arg_array['french'];
    	} else {
    		$error_msg .= '未输入维度法文<br />';
    	}

        if (isset($arg_array['chinese']) && !empty($arg_array['chinese'])) {
            $chinese = $arg_array['chinese'];
        } else {
            $error_msg .= '未输入维度中文<br />';
        }

        if (isset($arg_array['sortOrder']) && is_numeric($arg_array['sortOrder']) > 0) {
    		$sortOrder = $arg_array['sortOrder'];
    	} else {
    		$error_msg .= '请输入正确的排序序号<br />';
    	}
    
    	if (isset($arg_array['basic_array']) && is_array($arg_array['basic_array']) && !empty($arg_array['basic_array'])) {
    		$basic_array = $arg_array['basic_array'];
    	} else {
    		$error_msg .= '请输入基本信息<br />';
    	}
    
    	if (isset($arg_array['use_array']) && is_array($arg_array['use_array'])) {
    		$use_array = $arg_array['use_array'];
    	} else {
    		$error_msg .= '使用标志不正确<br />';
    	}
    	
    	if (isset($arg_array['basicVal']) && is_array($arg_array['basicVal'])) {
    		$basicVal = $arg_array['basicVal'];
    	}
    	
    	//判断基本信息是否有未填写的选项
    	foreach ($basic_array as $v) {
    		if (empty($v)) {
    			$error_msg .= '基本信息内有未填的选项<br />';
    			break;
    		}
    	}
    	
    	//判断数组中有没有重复的值
    	if (count($basic_array) != count(array_unique($basic_array))) {
    		return array( 'code' => 400 , 'message' => '商品基本信息值存在重复' );
    	}
    	
    	if ($error_msg != '') {
    		return array( 'code' => 400 , 'message' => $error_msg );
    	}

    	/**
    	 * 数据存入数据库
    	 */

        //存基本信息组
        $goods_basic_group = new entity('goods_basic_group');

        if ($basicGroupID > 0) {
            $goods_basic_group->basicGroupID = $basicGroupID;
            if (!$this->dbca->fetch($goods_basic_group)) {
                return array( 'code' => 400 , 'message' => '基本信息组ID不存在' );
            }
            $goods_basic_group->editor = $G_PGLOB->currentUserID;

            $allsortOrder = $this->dbca->stmt_query('SELECT sortOrder,chinese,french FROM goods_basic_group WHERE basicGroupID != ?','i',$basicGroupID);
        } else {
            $goods_basic_group->creator = $G_PGLOB->currentUserID;
            $allsortOrder = $this->dbca->stmt_query('SELECT sortOrder,chinese,french FROM goods_basic_group');
        }

        $msg = '';
        foreach ($allsortOrder as $k=>$v) {
            if ($v->sortOrder == $sortOrder) {
                $msg .= '排序序号已存在<br />';
            }
            if ($v->chinese == $chinese) {
                $msg .= '维度中文已存在<br />';
            }
            if ($v->french == $french) {
                $msg .= '维度法文已存在<br />';
            }
        }

        if (!empty($msg)) {
            return array( 'code' => 400 , 'message' => $msg );
        }

        $goods_basic_group->french      = $french;
        $goods_basic_group->chinese     = $chinese;
        $goods_basic_group->sortOrder   = $sortOrder;
        $goods_basic_group->useFlg      = 1;

    	try{
    		$this->dbca->beginTransaction();

    		if (!$this->dbca->save($goods_basic_group)) {
    			throw new Exception('基本信息组保存失败',400);
    		}

    		$flg = 0;   //添加的标志
    		if ($basicGroupID == 0) {
    			$basicGroupID = $this->dbca->insert_id;
    		} else {
				$basicNameInfo = $this->dbca->stmt_query('SELECT basicName FROM goods_basic WHERE basicGroupID = ?','i',$basicGroupID);
    			$flg = 1;   //修改的标志
    		}

            /**
             * 修改基本信息
             */
            if (isset($basicNameInfo) && !empty($basicNameInfo)) {
                foreach($basicNameInfo as $v) {
                    $basicName = $v->basicName;
                    if (in_array($basicName,$basic_array)) {
                        $goods_basic = new entity('goods_basic');
                        $goods_basic->basicGroupID = $basicGroupID;
                        $goods_basic->basicName = $basicName;
                        if (!$this->dbca->fetch($goods_basic)) {
                            throw new Exception ('没有此基本信息' , 404 );
                        }
                        $k = array_search($basicName,$basic_array);
                        $goods_basic->chinese   = $chinese;
                        $goods_basic->french    = $french;
                        $goods_basic->useFlg    = $use_array[$k];
                        $goods_basic->basicVal  = $basicVal[$k];

                        $this->dbca->save($goods_basic);

                        //将当前基本信息删除
                        unset($basic_array[$k]);
                        unset($use_array[$k]);

                    } else {
                        $this->dbca->stmt_query('DELETE FROM goods_basic WHERE basicGroupID = ? AND basicName = ?','is',array($basicGroupID,$basicName));
                    }
                }
            }

            /**
             * 添加基本信息
             */
            //获取商品基本信息的序号
            $basic_order = $this->dbca->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,1,MAX(sortOrder)+1) AS basic_order FROM goods_basic');
            $basic_order = $basic_order[0]->basic_order;

            //保存基本信息值
            foreach($basic_array as $k=>$v) {
                foreach($use_array as $k1=>$v1) {
                    if ($k == $k1) {
                        $goods_basic = new entity('goods_basic');
                        $goods_basic->basicName     = $v;
                        $goods_basic->useFlg        = $v1;
                        $goods_basic->basicVal      = isset($basicVal[$k1]) ? $basicVal[$k1] : '';
                        $goods_basic->creator       = $G_PGLOB->currentUserID;
                        $goods_basic->basicGroupID  = $basicGroupID;
                        $goods_basic->chinese       = $chinese;
                        $goods_basic->french        = $french;
                        $goods_basic->sortOrder     = $basic_order;

                        if (!$this->dbca->save($goods_basic)) {
                            throw new Exception('基本信息保存失败',400);
                        }

                        $basic_order++;
                    }
                }
            }

    		$this->dbca->commit();
    
    		return array( 'code' => 200 , 'message' => '数据保存成功' , 'data' => $flg );
    
    	} catch(Exception $e) {
    
    		$this->dbca->rollback();
    
    		if ($e->getCode() > 0) {
    			return array( 'code' => $e->getCode() , 'message' => $e->getMessage() );
    		} else {
    			error_log($e->getMessage());
    			return array( 'code' => 500 , 'message' => '系统错误' , 'data' => $flg );
    		}
    
    	}
    }
    
    /**
     * 商品信息维度的删除
     */
    public function delgoodsBasic($basicGroupID) {
    
    	try{
    
    		$this->dbca->beginTransaction();
    
    		$goods_basic_group = new entity('goods_basic_group');
    		$goods_basic_group->basicGroupID = $basicGroupID;
    		if (!$this->dbca->fetch($goods_basic_group)) {
    			throw new Exception('要删除的基本信息维度不存在',404);
    		} else {
    			if (!$this->dbca->delete($goods_basic_group)) {
    				throw new Exception('删除基本信息维度失败',400);
    			}
    		}
    
    		if (!$this->dbca->stmt_query('DELETE FROM goods_basic WHERE basicGroupID = ?','i',$basicGroupID)) {
    			throw new Exception('删除基本信息值失败',400);
    		}
    
    		$this->dbca->commit();
    
    		return array( 'code' => 200 , 'message' => 'ok' );
    
    	} catch (Exception $e) {
    
    		$this->dbca->rollback();
    
    		if ($e->getCode() > 0) {
    			return array( 'code' => $e->getCode() , 'message' => $e->getMessage() );
    		} else {
    			return array( 'code' => 500 , 'message' => '系统错误' );
    		}
    
    	}
    
    }
    
    /**
     * 商品信息维度的启用和禁用
     */
    public function changeuseFlg($basicGroupID,$useFlg) {
    
    	try{
    
    		//检测参数
    		$goods_basic_group = new entity('goods_basic_group');
    		$goods_basic_group->basicGroupID = $basicGroupID;
    		if (!$this->dbca->fetch($goods_basic_group)) {
    			throw new Exception( '查无此商品维度信息' , 404 );
    		}
    
    		if (!is_numeric($useFlg) || !in_array($useFlg,array(0,1))) {
    			throw new Exception( '使用状态不正确' , 404 );
    		}
    
    		//多页面操作问题
    		if ($useFlg == 1 && $goods_basic_group->useFlg == 1) {
    			throw new Exception( '商品维度已经启用,请刷新页面' , 400 );
    		}
    
    		if ($useFlg == 0 && $goods_basic_group->useFlg == 0) {
    			throw new Exception( '商品维度已经禁用,请刷新页面' , 400 );
    		}
    
    		//使用状态修改
    		$goods_basic_group->useFlg = $useFlg;
    
    		if (!$this->dbca->save($goods_basic_group)) {
    			throw new Exception( '使用状态修改失败' , 400 );
    		}
    
    		return array( 'code' => 200 , 'message' => '使用状态修改成功' );
    
    	} catch (Exception $e) {
    
    		if ($e->getCode() > 0) {
    			return array( 'code' => $e->getCode() , 'message' => $e->getMessage() );
    		} else {
    			return array( 'code' => 500 , 'message' => '系统错误' );
    		}
    
    	}
    }
    
    /**
     * 查询商品和基本信息映射关系
     * create by allen.qiang
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function searchBasicRelation( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //网购号
        if (isset($arg_array['postID']) && is_numeric($arg_array['postID'])) {
            $sql .= ' AND postID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['postID'];
        }

        //基本信息组ID
        if (isset($arg_array['basicGroupID']) && is_numeric($arg_array['basicGroupID'])) {
            $sql .= ' AND basicGroupID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['basicGroupID'];
        }
        
        //基本信息值ID
        if (isset($arg_array['basicID']) && is_numeric($arg_array['basicID'])) {
            $sql .= ' AND basicID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['basicID'];
        }
        
        //扩展
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
        	$sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_basic_relation ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_basic_relation ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'postID' => $item->postID,
                    'basicGroupID' => $item->basicGroupID,
                    'chinese' => $item->chinese,
                    'french' => $item->french,
                    'basicID' => $item->basicID,
                    'basicName' => $item->basicName,
                    'sortOrder' => $item->sortOrder,
		    'basicVal' => $item->basicVal
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }
    
    
    /**
     * 查询商品基本信息组表
     * create by allen.qiang
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function searchBasicGroup( &$arg_array ) {
    
    	//sql字符串
    	$sql = '';
    
    	//参数类型字符串
    	$params_type = '';
    
    	//参数值数组
    	$params_array = array();
    
    	//基本信息组ID
    	if (isset($arg_array['basicGroupID']) && is_numeric($arg_array['basicGroupID'])) {
    		$sql .= ' AND basicGroupID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['basicGroupID'];
    	}
    
    	//使用标志
    	if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
    		$sql .= ' AND useFlg = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['useFlg'];
    	}
    
    	//扩展
    	if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
    		$sql .= $arg_array['extend'];
    	}
    
    	if ($sql) {
    		$sql = 'WHERE ' . substr($sql, 4);
    	}
    
    	try {
    
    		if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
    
    			//统计数量
    			$search_sql = 'SELECT COUNT(*) AS item_count FROM goods_basic_group ' . $sql;
    			$count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    			$item_count = $count_array[0]->item_count;
    			if ($item_count == 0) {
    				$arg_array['count'] = 0;
    				return null;
    			}
    
    			$arg_array['count'] = $item_count;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    			//分页
    			if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
    				$arg_array['page'] = 1;
    
    			$sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
    
    		} else {
    
    			$arg_array['count'] = 0;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    		}
    
    		//查询分类信息
    		$search_sql = 'SELECT * FROM goods_basic_group ' . $sql;
    		$temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    		if (!$temp_item_array)
    			return null;
    
    		foreach($temp_item_array as &$item) {
    
    			$item_array[] = array(
    					'basicGroupID' => $item->basicGroupID,
    					'chinese' => $item->chinese,
    					'french' => $item->french,
    					'sortOrder' => $item->sortOrder,
    					'useFlg' => $item->useFlg
    			);
    
    		}
    
    		return $item_array;
    
    	} catch (Exception $e) {
    
    		error_log($e->getMessage());
    		return null;
    
    	}
    
    }
    
    /**
     * 查询商品基本信息值
     * create by allen.qiang
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function searchBasic( &$arg_array ) {
    
    	//sql字符串
    	$sql = '';
    
    	//参数类型字符串
    	$params_type = '';
    
    	//参数值数组
    	$params_array = array();
    
    	//基本信息组ID
    	if (isset($arg_array['basicGroupID']) && is_numeric($arg_array['basicGroupID'])) {
    		$sql .= ' AND basicGroupID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['basicGroupID'];
    	}
    
    	//基本信息值ID
    	if (isset($arg_array['basicID']) && is_numeric($arg_array['basicID'])) {
    		$sql .= ' AND basicID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['basicID'];
    	}
    
    	//基本信息值
    	if (isset($arg_array['basicName']) && is_numeric($arg_array['basicName'])) {
    		$sql .= ' AND basicName LIKE ?';
    		$params_type .= 's';
    		$params_array[] = '%' . $arg_array['basicName'] . '%';
    	}
    
    	//使用标志
    	if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
    		$sql .= ' AND useFlg = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['useFlg'];
    	}
    
    	//扩展
    	if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
    		$sql .= $arg_array['extend'];
    	}
    
    	if ($sql) {
    		$sql = 'WHERE ' . substr($sql, 4);
    	}
    
    	try {
    
    		if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
    
    			//统计数量
    			$search_sql = 'SELECT COUNT(*) AS item_count FROM goods_basic ' . $sql;
    			$count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    			$item_count = $count_array[0]->item_count;
    			if ($item_count == 0) {
    				$arg_array['count'] = 0;
    				return null;
    			}
    
    			$arg_array['count'] = $item_count;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    			//分页
    			if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
    				$arg_array['page'] = 1;
    
    			$sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
    
    		} else {
    
    			$arg_array['count'] = 0;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    		}
    
    		//查询分类信息
    		$search_sql = 'SELECT * FROM goods_basic ' . $sql;
    		$temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    		if (!$temp_item_array)
    			return null;
    
    		foreach($temp_item_array as &$item) {
    
    			$item_array[] = array(
    					'basicGroupID' => $item->basicGroupID,
    					'chinese' => $item->chinese,
    					'french' => $item->french,
    					'basicID' => $item->basicID,
    					'basicName' => $item->basicName,
    					'basicVal' => $item->basicVal,
    					'sortOrder' => $item->sortOrder,
    					'useFlg' => $item->useFlg
    			);
    
    		}
    
    		return $item_array;
    
    	} catch (Exception $e) {
    
    		error_log($e->getMessage());
    		return null;
    
    	}
    
    }
    
}